package com.data.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

@SuppressWarnings("ALL")
@Slf4j
@Component
public class AesUtil {


    private static String addUserUrl;

//    public static final String KEY = "ELxSeiCpPwcAJc4e";//密钥key
//    public static final String IV  = "1012132405963708";//向量iv

    public static  String aesKey ;//密钥key

    public static  String dbAesKey ;//密钥dekey
    public static  String aseIv  ;//向量iv

    @Value("${appaes.key:}")
    public void setAesKey(String aesKey) {
        AesUtil.aesKey = aesKey;
    }

    @Value("${appaes.iv:}")
    public void setAseIv(String aseIv) {
        AesUtil.aseIv = aseIv;
    }

    @Value("${appaes.dbkey:}")
    public void setDbAesKey(String dbAesKey) {
        AesUtil.dbAesKey = dbAesKey;
    }

    //aes 加密
    public static String aesEncrypt(String content) throws Exception {
        byte[] raw = aesKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
        //使用CBC模式，需要一个向量iv，可增加加密算法的强度
        IvParameterSpec ips = new IvParameterSpec(aseIv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);
        byte[] encrypted = cipher.doFinal(content.getBytes());
        return new BASE64Encoder().encode(encrypted);
    }

    //aes 解密
    public static String aesDecryption(String content) throws Exception {
        try {
            byte[] raw = aesKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(aseIv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    //dbaes 加密
    public static String dbAesEncrypt(String content) throws Exception {
        byte[] raw = dbAesKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
        //使用CBC模式，需要一个向量iv，可增加加密算法的强度
        IvParameterSpec ips = new IvParameterSpec(aseIv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ips);
        byte[] encrypted = cipher.doFinal(content.getBytes());
        return new BASE64Encoder().encode(encrypted);
    }

    //dbaes 解密
    public static String deAesDecryption(String content) throws Exception {
        try {
            byte[] raw = dbAesKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(aseIv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ips);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static void main(String []args) throws Exception {

        String password = "123456";
        String ss = aesEncrypt(password);

        System.out.println("===ss==="+ss);

    }


}
